/**
 * XDevice Raft测试可观测性分析工具
 * 
 * 提供高级分析功能：
 * 1. 事件序列分析
 * 2. 一致性验证
 * 3. 性能分析
 * 4. 故障模式检测
 * 5. 数据可视化导出
 */

#ifndef RAFT_OBSERVABILITY_H
#define RAFT_OBSERVABILITY_H

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

/* 分析结果类型 */
typedef enum {
    ANALYSIS_CONSISTENCY_OK = 0,
    ANALYSIS_CONSISTENCY_VIOLATION,
    ANALYSIS_PERFORMANCE_DEGRADATION,
    ANALYSIS_SPLIT_BRAIN_DETECTED,
    ANALYSIS_ELECTION_STORM,
    ANALYSIS_LOG_DIVERGENCE,
    ANALYSIS_NETWORK_ISSUE,
    ANALYSIS_NODE_FAILURE_PATTERN
} analysis_result_type_t;

/* 分析结果 */
typedef struct {
    analysis_result_type_t type;
    uint64_t timestamp_us;
    int severity;  // 1-5, 5最严重
    char description[256];
    char recommendation[512];
    int affected_nodes[16];
    int affected_node_count;
} analysis_result_t;

/* 性能分析结果 */
typedef struct {
    double avg_election_time_ms;
    double max_election_time_ms;
    double min_election_time_ms;
    double election_success_rate;
    
    double avg_heartbeat_interval_ms;
    double heartbeat_loss_rate;
    
    double avg_log_replication_latency_ms;
    double log_replication_throughput_ops_per_sec;
    
    double cluster_availability_percentage;
    double data_consistency_score;  // 0-1
    
    int total_leader_changes;
    int total_network_partitions;
    int total_node_failures;
} performance_analysis_t;

/* 一致性检查结果 */
typedef struct {
    bool log_consistency;
    bool term_consistency;
    bool state_consistency;
    bool commit_consistency;
    
    int inconsistent_nodes[16];
    int inconsistent_node_count;
    
    char violation_details[1024];
} consistency_check_t;

/* 初始化分析工具 */
int observability_init();

/* 清理分析工具 */
void observability_cleanup();

/* 记录事件用于分析 */
void observability_record_event(uint64_t timestamp_us, int event_type, 
                               int node_id, uint64_t term, 
                               const char* description, const char* details);

/* 记录节点状态快照 */
void observability_record_node_state(int node_id, uint64_t timestamp_us,
                                    int state, uint64_t term, int voted_for,
                                    uint64_t log_length, uint64_t commit_index);

/* 执行实时分析 */
int observability_analyze_realtime(analysis_result_t* results, int max_results);

/* 执行性能分析 */
performance_analysis_t observability_analyze_performance();

/* 执行一致性检查 */
consistency_check_t observability_check_consistency();

/* 生成测试报告 */
int observability_generate_report(const char* filename);

/* 导出事件日志为JSON格式 */
int observability_export_json(const char* filename);

/* 导出事件日志为CSV格式 */
int observability_export_csv(const char* filename);

/* 生成可视化数据 */
int observability_export_visualization_data(const char* filename);

/* 检测故障模式 */
int observability_detect_failure_patterns(analysis_result_t* patterns, int max_patterns);

/* 预测性分析 */
typedef struct {
    double leader_failure_probability;
    double network_partition_probability;
    double election_timeout_probability;
    uint64_t predicted_next_event_timestamp_us;
    char prediction_details[256];
} predictive_analysis_t;

predictive_analysis_t observability_predictive_analysis();

#ifdef __cplusplus
}
#endif

#endif /* RAFT_OBSERVABILITY_H */
